//
//  YLReservationRecordController.m
//  YLFinanceService
//
//  Created by she1323 on 2018/1/17.
//  Copyright © 2018年 she1323. All rights reserved.
//  预约记录

#import "YLReservationRecordController.h"
#import "YLReservationRecordCell.h"
#import "YLReservationRecordTitleCell.h"
#import "YLReservationRecordViewModel.h"
#import <MJRefresh.h>
#import "WHNetworkTool.h"
#import "YLReservationRecordModel.h"
#import "YLAccountTool.h"
#import "WHProgressHUD.h"
@interface YLReservationRecordController ()
@property (nonatomic, strong) YLReservationRecordViewModel *viewModel;
@property (nonatomic, assign) NSInteger index;
@end

@implementation YLReservationRecordController

- (YLReservationRecordViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLReservationRecordViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _index = 1;
    
    self.title = @"预约记录";
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLReservationRecordTitleCell class]) bundle:nil] forCellReuseIdentifier:[YLReservationRecordTitleCell cellID]];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLReservationRecordCell class]) bundle:nil] forCellReuseIdentifier:[YLReservationRecordCell cellID]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.index = 1;
        [weakSelf loadData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.index ++;
        [weakSelf loadData];
    }];
    self.tableView.mj_footer.hidden = YES;
    [self.tableView.mj_header beginRefreshing];
    [self.viewModel.wh_noMoreDataSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf endRefresh];
        [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
    [self.viewModel.wh_loadDataErrorSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf endRefresh];
    }];
}

- (void)endRefresh
{
//    self.tableView.mj_footer.hidden = NO;
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}

- (void)loadData
{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
    params[@"PageIndex"] = @(_index);
    params[@"MaxResultCount"] = @(10);
    __weak typeof(self) weakSelf = self;
    [[self.viewModel.requestCommand execute:params] subscribeNext:^(id  _Nullable x) {
        [weakSelf setEmpty];
        [weakSelf endRefresh];
        [weakSelf.tableView reloadData];
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    self.tableView.mj_footer.hidden = !self.viewModel.groups.count;
    return self.viewModel.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *items = self.viewModel.groups[indexPath.section];
    id item = items[indexPath.row];
    if ([item isKindOfClass:[YLReservationRecordHeaderItem class]]) {
       YLReservationRecordTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLReservationRecordTitleCell cellID] forIndexPath:indexPath];
        cell.item = item;
        return cell;
    } else {
        YLReservationRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLReservationRecordCell cellID] forIndexPath:indexPath];
        cell.section = indexPath.section;
        cell.item = item;
  
        cell.cancelBlock = ^(NSString *productid, NSInteger section) {
            NSString *url = [NSString stringWithFormat:@"%@Product/ProductReservationCancel", YLURL];
            
            [WHProgressHUD showStatus:@"取消预约"];
            [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:@{@"Id" : productid} success:^(id responseObject) {
                [WHProgressHUD dismiss];
                if ([responseObject[@"httpCode"] integerValue] == 200) {
                
                   NSArray *groups = self.viewModel.groups[section];
                    YLReservationRecordHeaderItem *headerItem = groups.firstObject;
                    headerItem.status = @"已取消";
                    YLReservationRecordItem *item = groups.lastObject;
                    item.showCancel = NO;
                    
                    [tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
                    
                    
                } else {
                    [WHProgressHUD showError:responseObject[@"message"]];
                }
            } fail:^(NSError *error) {
                
            }];
            
        };
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString * text = @"抱歉,暂时还没有预约记录!";
    
    NSDictionary * attributes = @ { NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f ],
        NSForegroundColorAttributeName:YLColor(130, 130, 130)};
    
    return [[ NSAttributedString  alloc ] initWithString:text attributes:attributes];
}


@end
